<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <p>count:{{count}}</p>
      <button @click="increase({amount:5})">+</button>
      <button @click="decrease">-</button>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script src="https://unpkg.com/vuex@3.1.3/dist/vuex.js"></script>
    <script>
      var store = new Vuex.Store({
        state:{count:1},
        mutations:{
          //类似于事件，有一个字符串事件类型，还有一个回调函数,此函数是唯一能够修改状态值的地方
          increase(state,payload={amount:1}){ //store.commit('increase');提交一个突变命令，变更操作
            state.count+=payload.amount; //mutation函数必须是同步的，里面不能写异步代码，定时器 ajax
          },
          decrease:function(state){
            state.count--;
          }
        }
      });
      var vm = new Vue({
        store,
        el:'#app',
        computed:{
           count(){
            return this.$store.state.count;
           }
        },
        methods:{
          //把store中的 mutations映射为Vue实例的方法
          ...Vuex.mapMutations(['increase','decrease']),
         /*  increase(){
            this.$store.commit('increase',{amount:5});
          },
          decrease(){
            this.$store.commit('decrease');
          } */
        }
      });
      //store.commit('increase',{amount:5});payload
      //Vuex规定不能直接修改state，在store中唯一的修改state的方法就是提交mutation
      //有点像事件，
    </script>
  </body>
</html>